package one.stand.controller;

import com.alibaba.fastjson.JSON;
import com.jmp.api.admin.dto.HomeLoginRequest;
import com.jmp.api.admin.dto.HomeLoginResponse;
import com.jmp.api.admin.dto.LoginCompanyRequest;
import com.jmp.api.admin.dto.LoginCompanyResponse;
import com.jmp.autocofiguration.util.OssUtil2;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.LoginAdminVo;
import com.jmp.base.dto.SecurityLoginAdminCompanyRequest;
import com.jmp.base.dto.SecurityLoginAdminDto;
import com.jmp.feign.SecurityFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.UserDto;
import one.stand.mapper.MenuMapperExt;
import one.stand.model.MenuModel;
import one.stand.model.ResultModel;
import one.stand.model.SysDictValue;
import one.stand.response.LoginResponse;
import one.stand.service.sys.SysDictValueService;
import one.stand.service.user.UserAdminService;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author: ZRH
 * @CreateDate: 2019/4/17 9:45
 */
@RestController
@RequestMapping("adminApi/home")
@Api(description = "登录")
@Slf4j
public class HomeController extends BaseController {
    @Autowired
    private SecurityFeign securityFeign;

    @Autowired
    private UserAdminService userAdminService;
    @Autowired
    private SysDictValueService sysDictValueService;
    @Autowired
    private MenuMapperExt menuMapperExt;

    @ApiOperation(value = "总后台登录")
    @PostMapping("login")
    public ResultModel<HomeLoginResponse> login(HomeLoginRequest request) {
        log.info("总后台登录：{}", JSON.toJSONString(request));
        ResultModel<LoginAdminVo> resultModel = securityFeign.loginAdmin(SecurityLoginAdminDto.builder()
                .loginName(request.getLoginName())
                .passWord(request.getPassWord())
                .build());
        AssertUtil.isTrue(resultModel);
        LoginAdminVo adminVo = resultModel.getData();

        UserDto userDto = userAdminService.get(adminVo.getUserId());

        // 加载菜单列
        List<MenuModel> modelList = menuMapperExt.selectList(new MenuModel() {{
            setAdminId(adminVo.getUserId());
        }});

        // result
        HomeLoginResponse response = HomeLoginResponse.builder()
                .token(adminVo.getToken())
                .ossUrl(OssUtil2.getWebsiteUrl())
                .adminId(userDto.getUserId())
                .loginName(userDto.getLoginName())
                .name(userDto.getName())
                .sex(userDto.getSex())
                .position(userDto.getPosition())
                .tourist(userDto.getTourist())
                .list(modelList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "企业登录")
    @PostMapping("login/company")
    @SecurityAuthCheck
    public ResultModel<LoginCompanyResponse> loginCompany(LoginCompanyRequest request) throws UnsupportedEncodingException {
        SecurityLoginAdminCompanyRequest loginAdminCompanyRequest = SecurityLoginAdminCompanyRequest.builder()
                .companyId(request.getCompanyId())
                .build();
        ResultModel<LoginResponse> result = securityFeign.loginAdminCompany(loginAdminCompanyRequest);
        AssertUtil.isTrue(result);
        String token = result.getData().getToken();
        String info = JSON.toJSONString(result.getData());

        SysDictValue sysDictValue = sysDictValueService.get(SysDictValueService.Type.CONFIG, SysDictValueService.TypeNameConfig.crm_pc_url.name());
        String redirectUrl = sysDictValue.getValue() + "/index.html?token=" + token + "&info=" + URLEncoder.encode(info, "UTF-8");
        LoginCompanyResponse response = LoginCompanyResponse.builder().redirectUrl(redirectUrl).build();
        return ResultModel.success(response);
    }
}
